package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserAnnotation;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.redis.Service.IStatisVoRedisService;
import cn.wolfcode.luowowo.redis.Service.IUserInfoRedisService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStatisVoRedisService statisVoRedisService;

    @Value("${sms.url}")
    private String ssmUrl;
    @Value("${appkey}")
    private String ssmAppkey;
    @Value("${sign}")
    private String ssmSign;

    @PostMapping("login")
    public Object login(String username , String password){
       //校验以及判断数据库中是否有该用户
        UserInfo userInfo = userInfoService.login(username,password);
        if (userInfo == null){
            return new JsonResult(JsonResult.CODE_ERROR_PARAM,"账号或者密码错误",null);
        }
        //存在的话创建唯一字符串token = UUID,token作为redis的key,userInfo作为value存入库
        //返回JsonResult,将token带到浏览器中
        String token = userInfoRedisService.getToken(userInfo);
        //浏览器需要token和user,需要封装成map对象才能带过去
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",userInfo);
        return JsonResult.success(map);

    }

    @GetMapping("currentUser")
    @RequireLogin
    public Object currentUser(HttpServletRequest req){
        //要获取当前登录用户信息
        //通过请求头获取token
        String token = req.getHeader("token");
        //通过token去获取当前用户信息
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        //判断返回的对象是否为空
        if(userInfo == null){
            return JsonResult.defaultError();
        }
        return JsonResult.success(userInfo);

    }

    @GetMapping("get")
    public Object get(String id ){
        return userInfoService.selectById(id);
    }

    @GetMapping("checkPhone")
    public boolean checkPhone(String phone){
        //如果通过手机号码找到用户,说明已经注册过
        boolean b = userInfoService.selectByPhone(phone);
        return !b;
    }


    @GetMapping("sendVerifyCode")
    public JsonResult sendVerifyCode(String phone){
        String code = userInfoService.sendVerifyCode(phone,ssmUrl,ssmAppkey,ssmSign);
        userInfoRedisService.sendVerifyCode(phone,code);
        return  JsonResult.success();
    }

    @PostMapping("regist")
    public JsonResult regist(String phone , String verifyCode,  String nickname,String password,String rpassword){
        //参数校验,以及添加新用户进入数据库
        userInfoService.regist( phone ,  verifyCode,  nickname, password, rpassword);
        return  JsonResult.success();

    }


    @GetMapping("strategies/favor")
    public JsonResult favor(String sid,String userId){
        //收藏回显
        List<String> sids = statisVoRedisService.getSids(sid, userId);
        return  JsonResult.success(sids);
    }


    @GetMapping("look")
    public JsonResult look(String  id, @UserAnnotation UserInfo userInfo){
        //通过id和当前登录用户的id作比较,判断是否是自己访问
        UserInfo user = userInfoRedisService.setView(id,userInfo);
        //只要前8个用户展现
        List<UserInfo> users = user.getUsers();
        if(users.size()>=8){
            List<UserInfo> userInfos = users.subList(0, 8);
            user.setUsers(userInfos);
        }
        return  JsonResult.success(user);
    }


    @GetMapping("lookAll")
    public JsonResult lookAll(String  id){
        //通过id查询用户
        UserInfo userInfo = userInfoService.selectById(id);
        return  JsonResult.success(userInfo);
    }


    @GetMapping("collection")
    public JsonResult collection(@UserAnnotation UserInfo userInfo){
        //通过用户id查询用户的攻略收藏
        String id = userInfo.getId();
        List<Strategy> strategies = userInfoRedisService.getStrategies(id);
        return JsonResult.success(strategies);
    }


}
